package com.example.extra.common;

import cn.hutool.core.collection.CollectionUtil;

import com.example.extra.entity.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author lixiang
 */
@Data
public class CustomUserDetails implements UserDetails {

    private Long id;
    private String username;
    private String password;
    private String nickname;
    private String avatar;
    private Integer status;
    private List<Long> categoryCodes;
    private Collection<? extends GrantedAuthority> authorities;
    private List<String> roles;
    private List<String> permissions;

    public CustomUserDetails(Long id, String username, String password, String nickname, String avatar,
                             Integer status, Collection<? extends GrantedAuthority> authorities,
                             List<Long> categoryCodes,
                             List<String> roles, List<String> permissions) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.nickname = nickname;
        this.avatar = avatar;
        this.status = status;
        this.categoryCodes = categoryCodes;
        this.authorities = authorities;
        this.roles = roles;
        this.permissions = permissions;
    }

    public static CustomUserDetails create(SysUser user, List<Long> categoryCodes , List<String> roles, List<String> permissions) {

        List<GrantedAuthority> authorities = new ArrayList<>();

        //添加类型
        if(CollectionUtil.isNotEmpty(categoryCodes)){
            categoryCodes.forEach(c->{
                authorities.add(new SimpleGrantedAuthority(String.valueOf(c)));
            });
        }

        // 添加角色
//        if(CollectionUtil.isNotEmpty(roles)){
//            roles.forEach(role -> {
//                authorities.add(new SimpleGrantedAuthority(role));
//                // 你也可以添加ROLE_前缀，取决于你的配置
//                // authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleCode()));
//            });
//        }


        // 添加权限
//        if(CollectionUtil.isNotEmpty(permissions)){
//            permissions.forEach(permission ->
//                    authorities.add(new SimpleGrantedAuthority(permission))
//            );
//        }



        return new CustomUserDetails(
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                user.getNickname(),
                user.getAvatar(),
                Integer.parseInt(user.getStatus()+""),
                authorities,
                categoryCodes,
                roles,
                permissions
        );
    }

    // 实现UserDetails接口的方法
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return status == 1;
    }

    // 自定义方法
    public Long getId() {
        return id;
    }

    public String getNickname() {
        return nickname;
    }

    public List<String> getRoles() {
        return roles;
    }

    public List<String> getPermissions() {
        return permissions;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
}
